/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package com.tivo.hme.bananas;

import com.tivo.hme.sdk.Resource;

public class BHighlightPlus extends BHighlight {
	protected int visible;
	protected int width;
	protected boolean isAbove;
	protected int dx;
	protected int dy;

	public BHighlightPlus(String name, Object action, int dx, int dy) {
		super(name, action, dx, dy);
        this.dx = dx;
        this.dy = dy;
        this.width = -1;
        this.isAbove = true;
        this.visible = H_VIS_FOCUS;
	}

	@Override
    public void refresh(BHighlights h, BScreen s, BRect r, Resource animation) {

		// We want to lock on the application monitor
		if ((lock == this) && (s != null)) {
			setMonitor(s.getBApp().getMonitor());
		}

        synchronized (lock) {
            int x = r.x + dx;
            int y = r.y + dy;

        	BView view = getView();
            if (getHighlightIsVisible(h, s, r, animation)) {
                if (view == null) {
                    // create the view
                    BSkin skin = s.getBApp().getSkin();
                    BView parent = isAbove ? s.getAbove() : s.getBelow();
    
                    // skin it (and maybe stretch it)
                    if (width == -1) {
                        setView(skin.createSkin(parent, getName(), x, y));
                    } else {
                    	setView(skin.createSkinStretch(parent, getName(), x, y, width));
                    }
                } else {
                    // show the existing view
                    view.setVisible(true);
                    view.setLocation(x, y, animation);  
                    if (width != -1 && width != view.getWidth()) {
                        view.setSize(width, view.getHeight(), animation);
                    }
                }
            } else if (view != null) {
                // hide it
                view.setVisible(false, animation);
                view.setLocation(x, y, animation);            
            }
        }
	}
	
	public boolean getHighlightIsVisible(BHighlights h, BScreen s, BRect r, Resource animation) {
		return h.getLayout().getHighlightIsVisible(visible);
	}

	@Override
    public void setStretchWidth(int width) {
        this.width = width;
        super.setStretchWidth(width);
    }

	@Override
	public void setAbove(boolean isAbove) {
    	this.isAbove = isAbove;
    	super.setAbove(isAbove);
	}

	@Override
    public void setVisible(int visible) {
        this.visible = visible;
        super.setVisible(visible);
    }

	public int getVisible() {
		return visible;
	}

	public int getStretchWidth() {
		return width;
	}

	public boolean isAbove() {
		return isAbove;
	}

	public int getDx() {
		return dx;
	}

	public int getDy() {
		return dy;
	}

	public void setDx(int dx) {
		this.dx = dx;
	}

	public void setDy(int dy) {
		this.dy = dy;
	}

}
